import { createApp, defineComponent } from "vue";
import { createPinia } from "pinia";
import { createFromIconfontCN } from "@ant-design/icons-vue";
import App from "./App.vue";
import { router, initRouters } from "./router";
import config from "./config";
import dayjs from "dayjs";
import directive from "./directive";
import 'dayjs/locale/zh-cn'
import "./axios-config";
import "./assets/main.less";
// unplugin-vue-components 插件不会自动引入message等样式 需手动引入
import 'ant-design-vue/es/message/style'
import { DefaultApi } from "./openapi-generator";
import { loadMenuList } from "./stores/menu";
import storage from "./storage";
import { StorageConst } from "./constant";

const init = async () => {
  const app = createApp(App);
  // 注册阿里图标
  const AliIcon = createFromIconfontCN({
    scriptUrl: "//at.alicdn.com/t/c/font_3794813_m2e5n36cwc.js"
  });

  // 挂载全局属性
  window.$config = app.config.globalProperties.$config = config;
  window.$adminApi = new DefaultApi(undefined, "");
  // 加载菜单
  try {
    const token = storage.getItem(StorageConst.Token);
    window._menuListCache = [];
    if (token) {
      window._menuListCache = (await loadMenuList())?.records || [];
      initRouters();
    }
  } catch (error) {}
  // 设置日期组件语言为中文
  dayjs.locale('zh-cn');
  // 初始化动态路由
  app.use(createPinia());
  app.use(router);
  // 自定义指令
  app.use(directive);
  app.component("AliIcon", AliIcon);
  app.mount("#app");
}

init();
